package com.imooc.demo.web;


import com.alibaba.fastjson.JSONObject;
import com.imooc.demo.config.appid.UserConstantInterface;
import com.imooc.demo.entity.UserAddressinfo;
import com.imooc.demo.entity.UserInfo;
import com.imooc.demo.service.UserInfoService;
import com.imooc.demo.util.HttpClientUtil;
import com.imooc.demo.util.IMoocJSONResult;

import com.imooc.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userinfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public Map<String, Object> user_login(@RequestParam("code") String code,
                                 @RequestParam("avatarUrl") String userHeadurl,
                                 @RequestParam("userName") String userNickname,
                                 @RequestParam("userGender") String userSex,
                                 @RequestParam("userCity") String userCity,
                                 @RequestParam("userProvince") String userProvince,
                                 @RequestParam("userCountry") String userCountry

) {

        System.out.println("code:"+code);
        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret", UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult=HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL,param);
        System.out.println(wxResult);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);

        // 获取参数返回的
        String session_key = jsonObject.get("session_key").toString();
        String userOpenid = jsonObject.get("openid").toString();
        System.out.println("session_key:"+session_key);
        System.out.println("userOpenid:"+userOpenid);

        // 根据返回的user实体类，判断用户是否是新用户，不是的话，更新数据，是的话，将用户信息存到数据库
        boolean userInfo = userInfoService.queryOpenidExist(userOpenid);
        System.out.println("userInfo:"+userInfo);
        if(userInfo==false){
            System.out.println("进来了");
            UserInfo insert_user = new UserInfo();
            insert_user.setUserNickname(userNickname);
            insert_user.setBuyerOpenid(userOpenid);
            insert_user.setUserHeadurl(userHeadurl);
            insert_user.setUserSex(userSex);
            insert_user.setUserCity(userCity);
            insert_user.setUserProvince(userProvince);
            insert_user.setUserCountry(userCountry);
            System.out.println("insert_user:"+insert_user.toString());
            // 添加到数据库
            Boolean flag = userInfoService.insertUserInfo(insert_user);
            if(!flag){
                System.out.println("插入数据库失败！");
                //return new IMoocJSONResult.errorMsg(ResultCode.FAIL);
            }else {
                System.out.println("插入数据库成功！");
            }

        }else {
            System.out.println("进来了,更新用户数据");
            UserInfo update_user = new UserInfo();
            update_user.setUserNickname(userNickname);
            update_user.setBuyerOpenid(userOpenid);
            update_user.setUserHeadurl(userHeadurl);
            update_user.setUserSex(userSex);
            update_user.setUserCity(userCity);
            update_user.setUserProvince(userProvince);
            update_user.setUserCountry(userCountry);
            System.out.println("update_user:"+update_user.toString());
            // 更新添加到数据库
            Boolean flag9 = userInfoService.updateUserInfo(update_user);
            if(!flag9){
                System.out.println("用户数据更新失败！");
                //return new IMoocJSONResult.errorMsg(ResultCode.FAIL);
            }else {
                System.out.println("用户数据更新成功！");
            }

        }

        UserInfo user = new UserInfo();
        user.setUserNickname(userNickname);
        user.setBuyerOpenid(userOpenid);
        user.setUserHeadurl(userHeadurl);
        user.setUserSex(userSex);
        user.setUserCity(userCity);
        user.setUserProvince(userProvince);
        user.setUserCountry(userCountry);

        // 封装返回小程序
        Map<String, Object> result = new HashMap<>();
       // result.put("session_key", session_key);
        result.put("userInfo", user);

        System.out.println("user:"+user);



        return  result;

    }




    // 一对多查询用户地址
    @RequestMapping(value = "getuserinfobyopenid",method = RequestMethod.GET)
    private  Map<String,Object>  getUserInfoByOpenid(String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<UserAddressinfo> userAddressinfo= userInfoService.getUserInfoByOpenid(buyerOpenid);
        modelMap.put("userAddressinfo",userAddressinfo);
        return modelMap;
    }




/*
    @RequestMapping(value = "/insertphone",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  insertPhone(@RequestParam("buyerOpenid") String buyerOpenid,
                                                       @RequestParam("userPhone") String userPhone,
                                                       @RequestParam("userName") String userName
                                                      ){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setUserPhone(userPhone);
        modelMap.put("success",userInfoService.insertPhone(userInfo));
        return modelMap;
    }

    @RequestMapping(value = "getuserindex",method = RequestMethod.GET)
    private  Map<String,Object>  getUserIndex(@RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        UserInfo userInfo= userInfoService.getUserIndex(buyerOpenid);
        modelMap.put("userInfo",userInfo);
        return modelMap;
    }

*/



}
